//
//  MyDevicesViewController.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/26.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class MyDevicesViewController: BaseViewController, BottomShadow, AddCornerUIRectCorner, UITableViewDataSource, UITableViewDelegate, YBPopupMenuDelegate, CameraAndPhotoPermissions, DelayExecution {

    let cellHeight: CGFloat = 122
    let topViewMargin: CGFloat = 10
    let contentTableViewMargin: CGFloat = 20
    let datas: [String] = ["a", "b"]
    @IBOutlet weak var addButton: UIButton!
    @IBOutlet weak var topViewHeight: NSLayoutConstraint!
    @IBOutlet weak var topView: UIView!
    @IBOutlet weak var bgImageView: UIImageView!
    @IBOutlet weak var maskView: BaseCircleView!
    @IBOutlet weak var headerImageView: BaseCircleImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var nameLabelWidth: NSLayoutConstraint!
    @IBOutlet weak var levelLabel: UILabel!
    @IBOutlet weak var sexImageView: UIImageView!
    @IBOutlet weak var contentTableView: UITableView!
    @IBOutlet weak var contentTableViewHeight: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        // Do any additional setup after loading the view.
    }
    
    func setUI() {
        bgImageView.layoutIfNeeded()
        titleLabel(text: "我的设备", nil)
        delayExecution(time: 0.1) {
            MyConfigTool.setBlurImage(self.bgImageView, alphaValue: 0.8)
        }
        setShadow(view: topView, opacity: bottomShadowOpacity, size: bottomShadowOffset, color: UIColor.black, cornerradius: CORNERRADIUS)
        addCornerWithView(aView: bgImageView, aCorners: .allCorners, aSize: CGSize(width: CORNERRADIUS, height: CORNERRADIUS))
        nameLabelWidth.constant += 20
        levelLabel.layer.cornerRadius = levelLabel.height / 2
        levelLabel.layer.borderColor = UIColor(hex: 0xEFEFEF).cgColor
        levelLabel.layer.borderWidth = 1
        contentTableView.register(UINib(nibName: "\(MyDevicesViewTableViewCell.self)", bundle: nil), forCellReuseIdentifier: "\(MyDevicesViewTableViewCell.self)")
        
        contentTableViewHeight.constant = CGFloat(datas.count) * cellHeight
    }
    
    override func setNavigation() {
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTap))
    }
    
    @objc func addTap() {
        let menuWidht: CGFloat = 126
        YBPopupMenu.show(at: CGPoint(x: ScreenWidth - 45, y: 54 + Ipx(false).topMargin), titles: ["扫一扫", "创建场景", "添加设备", "添加好友加入"], icons: ["icon_richscan", "icon_establish", "icon_addition", "icon_invitation"], menuWidth: menuWidht) { (popupMenu) in
            popupMenu?.cornerRadius = CORNERRADIUS;
            popupMenu?.showMaskView = true;
            popupMenu?.isShadowShowing = false;
            popupMenu?.arrowWidth = 15;
            //popupMenu?.selectIndex = _expressIndex;
            popupMenu?.delegate = self
            popupMenu?.offset = -5;
            popupMenu?.itemHeight = 44;
            popupMenu?.fontSize = 14;
            popupMenu?.textColor = ONColor.normalTextColor;
            popupMenu?.rectCorner = .allCorners;
        }
    }
    
    // MARK: - tableViewDatasource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "\(MyDevicesViewTableViewCell.self)") as? MyDevicesViewTableViewCell
        switch indexPath.row {
        case 0:
            cell?.showImageView.image = UIImage(named: "iocn_cycling_device")
            cell?.contentLabel.text = "骑行"
        default:
            cell?.showImageView.image = UIImage(named: "iocn_home_device")
            cell?.contentLabel.text = "家庭"
        }
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return cellHeight
    }
    
    // MARK: - tableViewDelegate
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        CustomShowAlertView().show(title: "是否接触关联该设备") { (isDone) in
        }
    }
    
    // MARK: - 添加按钮点击
    @IBAction func addButtonTap(_ sender: UIButton) {
        createScene()
    }
    
    // MARK: - YBPopupMenuDelegate
    func ybPopupMenuDidSelected(at index: Int, ybPopupMenu: YBPopupMenu!) {
        switch index {
        case 0:
            qrScan()
        case 1:
            createScene()
        case 2:
            addDevice()
        case 3:
            inviteFriends()
        default:
            break
        }
    }
    
    // MARK: - QRTap二维码
    func qrScan() {
        cameraPermissions(authorizedClosuer: {
            
            let QRCodeScanningVC = QRCodeScanningViewController()
            let navi = MainNavigaionViewController(rootViewController: QRCodeScanningVC)
            self.present(navi, animated: true, completion: nil)
        }, deniedClosuer: {
            CustomShowAlertView().show(title: "相机权限未开启", tapClosure: {
                let url = URL(string: UIApplicationOpenSettingsURLString)
                if let appUrl = url {
                    if UIApplication.shared.canOpenURL(appUrl) {
                        UIApplication.shared.openURL(appUrl)
                    }
                }
            })
        })
    }
    
    // MARK: - 创建场景
    func createScene() {
        let createSceneVC = CreateSceneViewController(nibName: "\(CreateSceneViewController.self)", bundle: nil)
        self.navigationController?.pushViewController(createSceneVC, animated: true)
    }
    
    // MARK: - 添加设备
    func addDevice() {
        let addDeviceVC = AddDeviceViewController(nibName: "\(AddDeviceViewController.self)", bundle: nil)
        navigationController?.pushViewController(addDeviceVC, animated: true)
    }

    @IBAction func headerTap(_ sender: UITapGestureRecognizer) {
//        let messageCirclePersonalCenterVC = MessageCirclePersonalCenterViewController(nibName: "\(MessageCirclePersonalCenterViewController.self)", bundle: nil, isMine: false)
//        
//        self.navigationController?.pushViewController(messageCirclePersonalCenterVC, animated: true)
    }
    
    // MARK: - 邀请好友
    func inviteFriends() {
        let inviteFriendsToJoinVC = InviteFriendsToJoinViewController(nibName: "\(InviteFriendsToJoinViewController.self)", bundle: nil)
         navigationController?.pushViewController(inviteFriendsToJoinVC, animated: true)
    }
}
